package com.lxmes.procontrol.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.JcIteminfoJs;
import com.lxmes.base.mapper.JcIteminfoJsMapper;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.mapper.CcRukuInfoMapper;
import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.procontrol.domain.NcProOrderInfo;
import com.lxmes.procontrol.mapper.NcProOrderInfoMapper;
import com.lxmes.sales.domain.*;
import com.lxmes.sales.mapper.*;
import com.lxmes.system.mapper.SysUserMapper;
import com.lxmes.technology.domain.NcProcessTec;
import com.lxmes.technology.mapper.NcProcessTecMapper;
import org.springframework.stereotype.Service;
import com.lxmes.procontrol.mapper.NcProProcessControlMapper;
import com.lxmes.procontrol.domain.NcProProcessControl;
import com.lxmes.procontrol.service.INcProProcessControlService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 生产过程控制总Service业务层处理
 *
 * @author 张志煜
 * @date 2022-11-26
 */
@Service
public class NcProProcessControlServiceImpl implements INcProProcessControlService
{
    @Resource
    private NcProProcessControlMapper ncProProcessControlMapper;

    @Resource
    private NcProcessproPlanMapper ncProcessproPlanMapper;
    @Resource
    private NcProOrderInfoMapper ncOrderInfoMapper;
    @Resource
    private NcProcessTecMapper tecMapper;
    @Resource
    private JcIteminfoJsMapper jsMapper;
    @Resource
    private JcIteminfoMapper iteminfoMapper;
    @Resource
    private CcRukuInfoMapper rukuInfoMapper;
    @Resource
    private SysUserMapper userMapper;

    @Resource
    private NcAssemblyFormalSchedulingPlanMapper ncAssemblyFormalSchedulingPlanMapper;

    @Resource
    private NcPackagingFormalSchedulingPlanMapper ncPackagingFormalSchedulingPlanMapper;

    @Resource
    private NcProcessingFormalSchedulingPlanMapper ncProcessingFormalSchedulingPlanMapper;

    @Resource
    private NcProductFormalSchedulingPlanMapper ncProductFormalSchedulingPlanMapper;


    @Resource
    private NcDistributionPlanMapper ncDistributionPlanMapper;

    @Resource
    private NcOrderResourcesYsMapper ncOrderResourcesYsMapper;





    /**
     * 查询生产过程控制总
     *
     * @param id 生产过程控制总主键
     * @return 生产过程控制总
     */
    @Override
    public NcProProcessControl selectNcProProcessControlById(Long id)
    {
        return ncProProcessControlMapper.selectNcProProcessControlById(id);
    }

    @Override
    public NcProProcessControl getOrderInfoPg(Long id) {
        NcProProcessControl control=  ncProProcessControlMapper.selectNcProProcessControlById(id);
        List<String> arrayList=new ArrayList<>();
        List<String> arrayList2=new ArrayList<>();
        List<String> arrayList3=new ArrayList<>();
        if(StringUtils.isNotEmpty(control.getGwPersonInfo()) || StringUtils.isNotEmpty(control.getPgGwFzr())){
            if(StringUtils.isNotEmpty(control.getGwPersonInfo()) ) {
                arrayList=new ArrayList<String>(Arrays.asList(control.getGwPersonInfo().split(",")));
                arrayList2 = new ArrayList<String>(Arrays.asList(control.getGwPersonInfo2().split(",")));
                arrayList3=new ArrayList<String>(Arrays.asList(control.getGwPersonInfo1().split(",")));
            }
            if (StringUtils.isNotEmpty(control.getPgGwFzr())){
                arrayList.add(control.getPgGwFzr());
                arrayList2.add(control.getPgGwFzr2());
                arrayList3.add(control.getPgGwFzr1());
            }

            arrayList= arrayList.stream().distinct().collect(Collectors.toList());
            arrayList2= arrayList2.stream().distinct().collect(Collectors.toList());
            arrayList3= arrayList3.stream().distinct().collect(Collectors.toList());
            //编号
            String[] userId= arrayList.toArray(new String[arrayList.size()]);
            //姓名
            String[] userName= arrayList2.toArray(new String[arrayList2.size()]);
            String[] userEmpNum= arrayList3.toArray(new String[arrayList3.size()]);
            //排产数量-派工数量 取余人数
            List<NcProOrderInfo> listUserOrderInfo=new ArrayList<>();
            BigDecimal[] results =(control.getPcNum().subtract(control.getPgNum())).divideAndRemainder(BigDecimal.valueOf(userId.length));
            for (int i = 0; i <userId.length ; i++) {
                NcProOrderInfo userOrderInfo=new NcProOrderInfo();
                //userOrderInfo.setId(control.getId());
                userOrderInfo.setEmpNum(userEmpNum[i]);
                userOrderInfo.setUserId(Long.valueOf(userId[i]));
                userOrderInfo.setEmpName(userName[i]);
                if(i==(userId.length-1)){
                    userOrderInfo.setPgNum(results[0].add(results[1]));
                }else {
                    userOrderInfo.setPgNum(results[0]);
                }
                listUserOrderInfo.add(userOrderInfo);
            }
            control.setListUserOrderInfo(listUserOrderInfo);
        }
        return control;
    }

    @Override
    @Transactional
    public int shControl(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            NcProProcessControl con = ncProProcessControlMapper.selectNcProProcessControlById(ids[i]);
            if("0".equals(con.getIsSh())){
                if(StringUtils.isNotEmpty(con.getLineCode())&& StringUtils.isNotEmpty(con.getPgGwFzr()) && StringUtils.isNotEmpty(con.getGwCode()) ){
                    NcProProcessControl control=new NcProProcessControl();
                    control.setId(ids[i]);
                    control.setIsSh("1");
                    ncProProcessControlMapper.updateNcProProcessControl(control);
                }else {
                    throw new ServiceException("审核的数据存在生产线或工位或派工负责人为空的项，请检查！！！");
                }
            }else {
                throw new ServiceException("审核的数据中存在已审核的数据，请检查！！");
            }
        }
        return 1;
    }

    public static void main(String[] args) {
        int a = 10, b = 2;
        System.out.println(a / b);
        System.out.println(a % b);
    }

    /**
     * 查询生产过程控制总列表
     *
     * @param ncProProcessControl 生产过程控制总
     * @return 生产过程控制总
     */
    @Override
    public List<NcProProcessControl> selectNcProProcessControlList(NcProProcessControl ncProProcessControl)
    {
        return ncProProcessControlMapper.selectNcProProcessControlList(ncProProcessControl);
    }

    /**
     * 新增生产过程控制总
     *
     * @param ncProProcessControl 生产过程控制总
     * @return 结果
     */
    @Override
    public int insertNcProProcessControl(NcProProcessControl ncProProcessControl)
    {
        return ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
    }
    @Override
    @Transactional
    public int addRukuToOrderInfo(Long id) {
        CcRukuInfo ruku = rukuInfoMapper.selectCcRukuInfoById(id);
        if (ruku != null) {
            if (!"J".equals(ruku.getClassId())) {
                throw new ServiceException("只支持基础料生成派工单");
            }
            JcIteminfoJs js = new JcIteminfoJs();
            js.setJdzhqItemId(ruku.getClassId());
            List<JcIteminfoJs> jsList = jsMapper.selectJcIteminfoJsList(js);
            js = jsList.get(0);
            NcProcessTec tec = new NcProcessTec();
            tec.setProLineCode(js.getLineCode());
            tec.setStationCode(js.getGwCode());
            List<NcProcessTec> list = tecMapper.selectNcProcessTecList(tec);
            if(list.size()>0){
                tec=list.get(0);
                NcProProcessControl ncProProcessControl = new NcProProcessControl();
                ncProProcessControl.setLineCode(tec.getProLineCode());
                ncProProcessControl.setLineName(tec.getProLineName());
                ncProProcessControl.setPcCode("TKJPC"+ NewNumber.createNewNumber(ncProcessproPlanMapper.getNewCode(DateUtils.getDate())));
                ncProProcessControl.setPcDate(new Date());
                ncProProcessControl.setDyYclCode(ruku.getItemId());
                ncProProcessControl.setProName(ruku.getItemName());
                ncProProcessControl.setFactoryCode("WR");
                ncProProcessControl.setFactoryName("玮瑞");
                ncProProcessControl.setGwCode(tec.getStationCode());
                ncProProcessControl.setGwName(tec.getStationName());
                ncProProcessControl.setPgGwCode(tec.getPgGwCode());
                ncProProcessControl.setPgGwName(tec.getPgGwName());
                ncProProcessControl.setPgGwFzr(tec.getPgGwFzr());
                ncProProcessControl.setPgGwFzr1(tec.getPgGwFzr1());
                ncProProcessControl.setPgGwFzr2(tec.getPgGwFzr2());
                ncProProcessControl.setGwPersonNum(tec.getGwPersonNum());
                ncProProcessControl.setGwPersonInfo(tec.getGwPersonInfo());
                ncProProcessControl.setGwPersonInfo1(tec.getGwPersonInfo1());
                ncProProcessControl.setGwPersonInfo2(tec.getGwPersonInfo2());
                ncProProcessControl.setPcNum(ruku.getRukuNum());
                ncProProcessControl.setNoPcNum(new BigDecimal("0"));
                ncProProcessControl.setOrderNum(new BigDecimal("0"));
                ncProProcessControl.setCreateTime(new Date());
                ncProProcessControl.setCreateBy(SecurityUtils.getUsername());
                ncProProcessControl.setIsSh("1");
                ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
            }else {
                throw new ServiceException("未找到生产线工位信息");
            }
            } else {
                throw new ServiceException("未找到入库单！！");
            }
            //回写入库单状态已生成
           ruku.setIsSc("1");
            rukuInfoMapper.updateCcRukuInfo(ruku);
            return 1;
        }


    @Override
    @Transactional
    public int addOrderInfoPg(NcProProcessControl ncProProcessControl) {
        NcProProcessControl con = ncProProcessControlMapper.selectNcProProcessControlById(ncProProcessControl.getId());
        String danhao= "PG"+ NewNumber.createNewNumber(ncOrderInfoMapper.getNewCode(DateUtils.getDate()));
        List<NcProOrderInfo> list = ncProProcessControl.getListUserOrderInfo();
        SysUser user =new SysUser();
        for (NcProOrderInfo info:list){
            if(info.getPgNum().compareTo(new BigDecimal("0"))>0) {
                info.setDanhao(danhao);
                user= userMapper.selectUserById(info.getUserId());
                if(user==null){
                    info.setDeptId(SecurityUtils.getDeptId());
                }else {
                    info.setDeptId(user.getDeptId());
                }
                info.setCreateTime(new Date());
                info.setCreateBy(SecurityUtils.getUsername());
                info.setFactoryCode(SecurityUtils.getFactoryCode());
                info.setFactoryName(SecurityUtils.getFactoryName());
                info.setOrderNo(con.getOrderNo());
                info.setContractNo(con.getContractNo());
                info.setPcCode(con.getPcCode());
                info.setPcDate(con.getPcDate());
                info.setScDate(con.getScDate());
                info.setJhDate(con.getJhDate());
                info.setLineCode(con.getLineCode());
                info.setLineName(con.getLineName());
                info.setGwCode(con.getGwCode());
                info.setGwName(con.getGwName());
                info.setProType(con.getProType());
                info.setGxProCode(con.getGxProCode());
                info.setProName(con.getProName());
                info.setDyProCode(con.getDyProCode());
                info.setDyYclCode(con.getDyYclCode());
                info.setPcNum(con.getPcNum());
                info.setReturnNum(con.getReturnNum());
                info.setItemIdentifyCode(con.getItemIdentifyCode());
                info.setWjgc(con.getWjgc());
                info.setNjgc(con.getNjgc());
                info.setHdgc(con.getHdgc());
                info.setTjtg(con.getTjtg());
                info.setZxj(con.getZxj());
                info.setKjks(con.getKjks());
                info.setLlzl(con.getLlzl());
                info.setXlzl(con.getXlzl());
                info.setJszl(con.getJszl());
                info.setContent(con.getContent());
                info.setDj(con.getDj());
                info.setSx(con.getSx());
                info.setMfmdj(con.getMfmdj());
                con.setPgNum(con.getPgNum().add(info.getPgNum()));
                //生成领料计划 除原材料和基础料成品
               /*
                JcIteminfoJs js=new JcIteminfoJs();
                js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(info.getLineCode(),info.getGwCode());
               if(!"Y".equals(js.getJdzhqItemId()) && !"C".equals(js.getJdzhqItemId()) && !"J".equals(js.getJdzhqItemId())){
                    JcIteminfo item = iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(info.getDyYclCode(),js.getJdzhqItemId(),info.getItemIdentifyCode());
                    //转换后的物料信息
                    CcPlanItemGet get=new CcPlanItemGet();
                    get.setGwCode(info.getGwCode());
                    get.setItemId(item.getItemId());
                    get.setOrderDanhao(info.getDanhao());
                    List<CcPlanItemGet> getlist = getMapper.selectCcPlanItemGetList(get);
                    if(getlist.size()==0){
                        String lyjh= "LYJH"+ NewNumber.createNewNumber(getMapper.getNewCode(DateUtils.getDate()));
                        BeanUtils.copyProperties(item,get);
                        get.setJhNum(info.getPgNum());
                        get.setChukuType(4L);
                        get.setGwCode(info.getGwCode());
                        get.setGwName(info.getGwName());
                        get.setLyDanhao(lyjh);
                        get.setCreateBy(SecurityUtils.getUsername());
                        get.setCreateTime(new Date());
                        get.setOrderDanhao(info.getDanhao());
                        getMapper.insertCcPlanItemGet(get);
                    }else {
                        get=getlist.get(0);
                        get.setJhNum(get.getJhNum().add(info.getPgNum()));
                        getMapper.updateCcPlanItemGet(get);
                    }
                }*/
                 JcIteminfoJs  js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(info.getLineCode(),info.getGwCode());
                if(!"Y".equals(js.getJdzhqItemId())&& !"J".equals(js.getJdzhqItemId())){
                    JcIteminfo jcIteminfo=new JcIteminfo();
                    //转换前的物料
                    jcIteminfo=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(null,js.getJdzhqItemId(),ncProProcessControl.getItemIdentifyCode());
                    info.setDyYclCode(jcIteminfo.getItemCode());
                    //转换后的物料信息
                    jcIteminfo=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(null,js.getJdzhhItemId(),ncProProcessControl.getItemIdentifyCode());
                    info.setDyProCode(jcIteminfo.getItemCode());
                }
                ncOrderInfoMapper.insertNcProOrderInfo(info);
            }
       }
         ncProProcessControl.setPgNum(con.getPgNum());
         ncProProcessControlMapper.updateNcProProcessControl(ncProProcessControl);

         return 1;

    }
/*
    public int addOrderInfoPg1(NcProProcessControl ncProProcessControl) {
        NcProProcessControl con = ncProProcessControlMapper.selectNcProProcessControlById(ncProProcessControl.getId());
        String danhao= "PG"+ NewNumber.createNewNumber(orderInfoMapper.getNewCode(DateUtils.getDate()));
        List<NcProOrderInfo> list = ncProProcessControl.getListUserOrderInfo();
        for (NcProOrderInfo info:list){
            if(info.getPgNum().compareTo(new BigDecimal("0"))>0) {
                info.setDanhao(danhao);
                //info.setUserId(info.getUserId());
                info.setDeptId(SecurityUtils.getDeptId());
                info.setCreateTime(new Date());
                info.setCreateBy(SecurityUtils.getUsername());
                info.setFactoryCode(con.getFactoryCode());
                info.setFactoryName("泰康");
                info.setOrderNo(con.getOrderNo());
                info.setContractNo(con.getContractNo());
                info.setPcCode(con.getPcCode());
                info.setPcDate(con.getPcDate());
                info.setScDate(con.getScDate());
                info.setJhDate(con.getJhDate());
                info.setLineCode(con.getLineCode());
                info.setLineName(con.getLineName());
                info.setGwCode(con.getGwCode());
                info.setGwName(con.getGwName());
                info.setProType(con.getProType());
                info.setGxProCode(con.getGxProCode());
                info.setProName(con.getProName());
                info.setDyProCode(con.getDyProCode());
                info.setDyYclCode(con.getDyYclCode());
                info.setPcNum(con.getPcNum());
                info.setReturnNum(con.getReturnNum());
                info.setItemIdentifyCode(con.getItemIdentifyCode());
                info.setWjgc(con.getWjgc());
                info.setNjgc(con.getNjgc());
                info.setHdgc(con.getHdgc());
                info.setTjtg(con.getTjtg());
                info.setZxj(con.getZxj());
                info.setKjks(con.getKjks());
                info.setLlzl(con.getLlzl());
                info.setXlzl(con.getXlzl());
                con.setPgNum(con.getPgNum().add(info.getPgNum()));
                //生成领料计划 不含原材料
                JcIteminfoJs js=new JcIteminfoJs();
                js.setLineCode(info.getLineCode());
                js.setGwCode(info.getGwCode());
                List<JcIteminfoJs> jsList = jsMapper.selectJcIteminfoJsList(js);
                js=jsList.get(0);
                //转后前的物料
                JcIteminfo jcIteminfo=new JcIteminfo();
                jcIteminfo.setClassId(js.getJdzhqItemId());
                jcIteminfo.setItemIdentifyCode(info.getItemIdentifyCode());
                jcIteminfo.setItemId(info.getDyYclCode());
                List<JcIteminfo> jcIteminfoList = iteminfoMapper.selectJcIteminfoList(jcIteminfo);
                if(jcIteminfoList.size()==0){
                    throw new ServiceException(info.getItemIdentifyCode()+"物料识别码未找到该物料信息！！！");
                }else if(jcIteminfoList.size()>1){
                    throw new ServiceException("物料数据不能重复！！！");
                }
                jcIteminfo=jcIteminfoList.get(0);
                info.setDyYclCode(jcIteminfo.getItemCode());
                if(!"Y".equals(js.getJdzhqItemId()) && !"C".equals(js.getJdzhqItemId()) && !"J".equals(js.getJdzhqItemId())){
                    //转换后的物料信息
                    JcIteminfo jcIteminfo2=new JcIteminfo();
                    jcIteminfo2.setClassId(js.getJdzhhItemId());
                    jcIteminfo2.setItemIdentifyCode(info.getItemIdentifyCode());
                    List<JcIteminfo> jcIteminfoList2 = iteminfoMapper.selectJcIteminfoList(jcIteminfo);
                    if(jcIteminfoList2.size()==0){
                        throw new ServiceException(info.getItemIdentifyCode()+"物料识别码未找到该物料信息！！！");
                    }else if(jcIteminfoList2.size()>1){
                        throw new ServiceException("物料数据不能重复！！！");
                    }
                    jcIteminfo2=jcIteminfoList2.get(0);
                    info.setDyProCode(jcIteminfo2.getItemCode());
                    CcPlanItemGet get=new CcPlanItemGet();
                    get.setGwCode(info.getGwCode());
                    get.setItemId(jcIteminfo.getItemId());
                    get.setOrderDanhao(info.getDanhao());
                    List<CcPlanItemGet> getlist = getMapper.selectCcPlanItemGetList(get);
                    if(getlist.size()==0){
                        String lyjh= "LYJH"+ NewNumber.createNewNumber(getMapper.getNewCode(DateUtils.getDate()));
                        BeanUtils.copyProperties(jcIteminfo,get);
                        get.setJhNum(info.getPgNum());
                        get.setChukuType(4L);
                        get.setGwCode(info.getGwCode());
                        get.setGwName(info.getGwName());
                        get.setLyDanhao(lyjh);
                        get.setCreateBy(SecurityUtils.getUsername());
                        get.setCreateTime(new Date());
                        get.setOrderDanhao(info.getDanhao());
                        getMapper.insertCcPlanItemGet(get);
                    }else {
                        get=getlist.get(0);
                        get.setJhNum(get.getJhNum().add(info.getPgNum()));
                        getMapper.updateCcPlanItemGet(get);
                    }

                }
                orderInfoMapper.insertNcProOrderInfo(info);
            }
        }
        ncProProcessControl.setPgNum(con.getPgNum());
        ncProProcessControlMapper.updateNcProProcessControl(ncProProcessControl);

        return 1;

    }
*/

    /**
     * 修改生产过程控制总
     *
     * @param ncProProcessControl 生产过程控制总
     * @return 结果
     */
    @Override
    public int updateNcProProcessControl(NcProProcessControl ncProProcessControl)
    {
        ncProProcessControl.setUpdateTime(DateUtils.getNowDate());
        ncProProcessControl.setUpdateBy(SecurityUtils.getUsername());
        return ncProProcessControlMapper.updateNcProProcessControl(ncProProcessControl);
    }

    /**
     * 批量删除生产过程控制总
     *
     * @param ids 需要删除的生产过程控制总主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNcProProcessControlByIds(Long[] ids)
    {
        for (int i = 0; i <ids.length ; i++) {
            NcProProcessControl con = ncProProcessControlMapper.selectNcProProcessControlById(ids[i]);
            if("0".equals(con.getIsSh())) {
                ncProProcessControlMapper.deleteNcProProcessControlById(ids[i]);
                List<NcProProcessControl> ncProProcessControls = ncProProcessControlMapper.selectNcProPC(con.getPlanId());
                if(ncProProcessControls.size()==0){
                    //解排产封印
                    NcProcessproPlan plan = new NcProcessproPlan();
                    plan.setId(con.getPlanId());
                    plan.setIsRead("0");
                    ncProcessproPlanMapper.updateNcProcessproPlan(plan);
                }
            }else {
                throw new ServiceException("删除的数据存在已审核的数据，删除失败！！！");
              }
            }
        return  1;
    }

    /**
     * 删除生产过程控制总信息
     *
     * @param id 生产过程控制总主键
     * @return 结果
     */
    @Override
    public int deleteNcProProcessControlById(Long id)
    {
        return ncProProcessControlMapper.deleteNcProProcessControlById(id);
    }

//    @Override
//    @Transactional
//    public int handRead() {
//        List<NcProcessproPlan> ncProcessproPlans = ncProcessproPlanMapper.readPcPlanList();
//        for (NcProcessproPlan plan : ncProcessproPlans) {
//            NcProcessTec tecQ=new NcProcessTec();
//            tecQ.setProLineCode(plan.getLineCode());
//            List<NcProcessTec> list = tecMapper.selectNcProcessTecList(tecQ);
//            for (NcProcessTec tec:list) {
//                //隔条工位不派工
//                if("TKSCX010002".equals(tec.getStationCode())){
//                    continue;
//                }
//            NcProProcessControl ncProProcessControl = new NcProProcessControl();
//            BeanUtils.copyProperties(plan, ncProProcessControl);
//            if (StringUtils.isNotEmpty(ncProProcessControl.getItemIdentifyCode())) {
//                JcItembzinfo bz = itembzinfoMapper.selectJcItembzinfoByItemIdentifyCode(ncProProcessControl.getItemIdentifyCode());
//                if (bz != null) {
//                   /* ncProProcessControl.setWjgc(bz.getFlwjD() + "±" + "0.5");
//                    ncProProcessControl.setNjgc(bz.getFlnj() + "±" + "0.5");
//                    ncProProcessControl.setHdgc(bz.getFlhdC() + "±" + "0.5");
//                    ncProProcessControl.setTjtg(bz.getFlmfmccD() + "*" + bz.getFltgF());
//                    ncProProcessControl.setZxj(bz.getFlkzxyzjK() + "");
//                    ncProProcessControl.setKjks(bz.getFlkjL() + "*" + bz.getFlksN());
//                    //ncProProcessControl.setLlzl(bz.getLlzl().setScale(2,BigDecimal.ROUND_HALF_UP) + "");
//                    ncProProcessControl.setJszl(bz.getJszl()+ "");
//                    ncProProcessControl.setXlzl(bz.getXlzl() + "");*/
//                    ncProProcessControl.setWjgc(bz.getFlwjD().setScale(1,BigDecimal.ROUND_DOWN) + "±" + "0.5");
//                    ncProProcessControl.setNjgc(bz.getFlnj().setScale(1,BigDecimal.ROUND_DOWN) + "±" + "0.5");
//                    ncProProcessControl.setHdgc(bz.getFlhdC().setScale(1,BigDecimal.ROUND_DOWN) + "±" + "0.5");
//                    ncProProcessControl.setTjtg(bz.getFlmfmccD().setScale(1,BigDecimal.ROUND_DOWN) + "*" + bz.getFltgF().setScale(1,BigDecimal.ROUND_DOWN));
//                    ncProProcessControl.setZxj(bz.getFlkzxyzjK().setScale(1,BigDecimal.ROUND_DOWN) + "");
//                    ncProProcessControl.setKjks(bz.getFlkjL().setScale(1,BigDecimal.ROUND_DOWN) + "*" + bz.getFlksN().setScale(0,BigDecimal.ROUND_HALF_UP));
//                    ncProProcessControl.setJszl(bz.getJszl().setScale(1,BigDecimal.ROUND_DOWN) + "");
//                    ncProProcessControl.setXlzl(bz.getXlzl().setScale(1,BigDecimal.ROUND_DOWN) + "");
//                } else {
//                    throw new ServiceException("物料识别码："+ncProProcessControl.getItemIdentifyCode()+"未找到标准物料信息！！！");
//                }
//            } else {
//                throw new ServiceException("排产："+ncProProcessControl.getPcCode()+"物料识别码不能为空");
//            }
//            ncProProcessControl.setPlanId(ncProProcessControl.getId());
//            ncProProcessControl.setId(null);
//            ncProProcessControl.setDyProCode(null);
//            ncProProcessControl.setDyYclCode(null);
//            ncProProcessControl.setGwCode(tec.getStationCode());
//            ncProProcessControl.setGwName(tec.getStationName());
//            ncProProcessControl.setPgGwCode(tec.getPgGwCode());
//            ncProProcessControl.setPgGwName(tec.getPgGwName());
//            ncProProcessControl.setPgGwFzr(tec.getPgGwFzr());
//            ncProProcessControl.setPgGwFzr1(tec.getPgGwFzr1());
//            ncProProcessControl.setPgGwFzr2(tec.getPgGwFzr2());
//            ncProProcessControl.setGwPersonNum(tec.getGwPersonNum());
//            ncProProcessControl.setGwPersonInfo(tec.getGwPersonInfo());
//            ncProProcessControl.setGwPersonInfo1(tec.getGwPersonInfo1());
//            ncProProcessControl.setGwPersonInfo2(tec.getGwPersonInfo2());
//            ncProProcessControl.setPcNum(new BigDecimal(plan.getPcNum()));
//            ncProProcessControl.setNoPcNum(new BigDecimal(plan.getNoPcNum()));
//            ncProProcessControl.setOrderNum(new BigDecimal(plan.getOrderNum()));
//            ncProProcessControl.setCreateTime(new Date());
//            ncProProcessControl.setCreateBy(SecurityUtils.getUsername());
//            ncProProcessControl.setRemark(null);
//                //物料转换规则找对应关系 排除原材料和基础料
//               /* JcIteminfoJs  js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(ncProProcessControl.getLineCode(),ncProProcessControl.getGwCode());
//                if(!"Y".equals(js.getJdzhqItemId())&& !"J".equals(js.getJdzhqItemId())){
//                    JcIteminfo jcIteminfo=new JcIteminfo();
//                    //转换前的物料
//                    jcIteminfo=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(null,js.getJdzhqItemId(),ncProProcessControl.getItemIdentifyCode());
//                    ncProProcessControl.setDyYclCode(jcIteminfo.getItemCode());
//                    //转换后的物料信息
//                    jcIteminfo=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(null,js.getJdzhhItemId(),ncProProcessControl.getItemIdentifyCode());
//                    ncProProcessControl.setDyProCode(jcIteminfo.getItemCode());
//                }*/
//            ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
//        }
//            plan.setIsRead("1");
//            ncProcessproPlanMapper.updateNcProcessproPlan(plan);
//    }
//        return 1;
//    }

    @Override
    @Transactional
    public int handRead() {
        //成品
        NcProductFormalSchedulingPlan ncProductFormalSchedulingPlan = new NcProductFormalSchedulingPlan();
        ncProductFormalSchedulingPlan.setIsRead("0");
        List<NcProductFormalSchedulingPlan> ncProductFormalSchedulingPlans = ncProductFormalSchedulingPlanMapper.selectNcProductFormalSchedulingPlanList(ncProductFormalSchedulingPlan);
        //总成
        NcPackagingFormalSchedulingPlan ncPackagingFormalSchedulingPlan = new NcPackagingFormalSchedulingPlan();
        ncPackagingFormalSchedulingPlan.setIsRead("0");
        List<NcPackagingFormalSchedulingPlan> ncPackagingFormalSchedulingPlans = ncPackagingFormalSchedulingPlanMapper.selectNcPackagingFormalSchedulingPlanList(ncPackagingFormalSchedulingPlan);
        //部装
        NcAssemblyFormalSchedulingPlan ncAssemblyFormalSchedulingPlan = new NcAssemblyFormalSchedulingPlan();
        ncAssemblyFormalSchedulingPlan.setIsRead("0");
        List<NcAssemblyFormalSchedulingPlan> ncAssemblyFormalSchedulingPlans = ncAssemblyFormalSchedulingPlanMapper.selectNcAssemblyFormalSchedulingPlanList(ncAssemblyFormalSchedulingPlan);
        //零件
        NcProcessingFormalSchedulingPlan ncProcessingFormalSchedulingPlan = new NcProcessingFormalSchedulingPlan();
        ncProcessingFormalSchedulingPlan.setIsRead("0");
        List<NcProcessingFormalSchedulingPlan> ncProcessingFormalSchedulingPlans = ncProcessingFormalSchedulingPlanMapper.selectNcProcessingFormalSchedulingPlanList(ncProcessingFormalSchedulingPlan);

        String username = SecurityUtils.getUsername();

        ncProductFormalSchedulingPlans.stream().forEach(productFormal->{
            NcProProcessControl ncProProcessControl = new NcProProcessControl();
            ncProProcessControl.setProType("成品");
            ncProProcessControl.setPcCode(productFormal.getPreProductionNumber());
            ncProProcessControl.setOrderNo(productFormal.getOrderNo());
            ncProProcessControl.setContractNo(productFormal.getContract());
            ncProProcessControl.setPcDate(productFormal.getOfficialProductionDate());
            ncProProcessControl.setScDate(productFormal.getOfficialProductionDate());
            ncProProcessControl.setJhDate(productFormal.getJhDate());
            ncProProcessControl.setLineCode(productFormal.getLineId());
            ncProProcessControl.setLineName(productFormal.getLineName());
            ncProProcessControl.setGwCode(productFormal.getGwCode());
            ncProProcessControl.setGwName(productFormal.getGwName());
            ncProProcessControl.setDyZcNum(productFormal.getDyZcNum());
            if(productFormal.getNormalProductionCount()==null){
                ncProProcessControl.setPcNum(new BigDecimal(0));
                ncProProcessControl.setNoPcNum(new BigDecimal(0));

            }else{
                ncProProcessControl.setPcNum(new BigDecimal(productFormal.getNormalProductionCount()));
                ncProProcessControl.setNoPcNum(new BigDecimal(productFormal.getNormalProductionCount()));
            }

            if(productFormal.getNormalProductionCount()==null){
                ncProProcessControl.setOrderNum(new BigDecimal(0));
            }else{
                ncProProcessControl.setOrderNum(new BigDecimal(productFormal.getOrderNum()));
            }

            ncProProcessControl.setPlanOnlineTime(productFormal.getPlanOnlineTime());
            ncProProcessControl.setPlanOfflineTime(productFormal.getPlanOfflineTime());
            ncProProcessControl.setCreateTime(new Date());
            ncProProcessControl.setCreateBy(username);
            ncProProcessControl.setRemark(null);
            ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
            ncProductFormalSchedulingPlan.setIsRead("1");
            ncProductFormalSchedulingPlan.setId(productFormal.getId());
            ncProductFormalSchedulingPlanMapper.updateNcProductFormalSchedulingPlan(ncProductFormalSchedulingPlan);


            List<CcStockInfoVo> stockInfos = ncOrderResourcesYsMapper.getStockInfoBySubCode(productFormal.getGwItemCode());

            String kwdm = "";
            for(int i=0;i<stockInfos.size();i++){
                if(i==stockInfos.size()-1){
                    kwdm += stockInfos.get(i).getKuweiCode();
                }else{
                    kwdm += stockInfos.get(i).getKuweiCode()+",";
                }
            }
            NcDistributionPlan ncDistributionPlan = new NcDistributionPlan();
            ncDistributionPlan.setFactoryCode(productFormal.getFactoryCode());
            ncDistributionPlan.setFactoryName(productFormal.getFactoryName());
            ncDistributionPlan.setOrderType(productFormal.getOrderType());
            ncDistributionPlan.setOrderNumber(productFormal.getOrderNo());
            ncDistributionPlan.setProductCode(productFormal.getProductNumber());
            ncDistributionPlan.setProductName(productFormal.getProductName());
            ncDistributionPlan.setOrderQuantity(productFormal.getOrderNum());
            ncDistributionPlan.setProductType(productFormal.getProType());
            ncDistributionPlan.setMaterialCode(productFormal.getGwItemCode());
            ncDistributionPlan.setMaterialName(productFormal.getGwItemName());
//            ncDistributionPlan.setMaterialSpecification(productFormal.getItemSpec());
            if(stockInfos.size()>0){
                ncDistributionPlan.setQualityStatus(stockInfos.get(0).getQualityState());
                ncDistributionPlan.setUsageStatus(stockInfos.get(0).getUseState());
                ncDistributionPlan.setWarehouseCode(stockInfos.get(0).getStockId());
            }
            ncDistributionPlan.setUnit(productFormal.getUnit());
            ncDistributionPlan.setTotalOrderUsage(BigDecimal.valueOf(productFormal.getOrderNum()*productFormal.getDtyl()));
            ncDistributionPlan.setPerUnitUsage(BigDecimal.valueOf(productFormal.getDtyl()));
            ncDistributionPlan.setDistributionPlanQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
            ncDistributionPlan.setLockedAccountQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
//            ncDistributionPlan.setLockedAccountTime(stockInfos.get(0).lo);
            ncDistributionPlan.setLocationCode(kwdm);
            ncDistributionPlan.setCreateBy(SecurityUtils.getUsername());
            ncDistributionPlan.setCreateTime(new Date());
            ncDistributionPlanMapper.insertNcDistributionPlan(ncDistributionPlan);
        });

        ncPackagingFormalSchedulingPlans.stream().forEach(packagingFormal->{
            NcProProcessControl ncProProcessControl = new NcProProcessControl();
            ncProProcessControl.setProType("总成");
            ncProProcessControl.setPcCode(packagingFormal.getPreProductionNumber());
            ncProProcessControl.setOrderNo(packagingFormal.getOrderNo());
            ncProProcessControl.setContractNo(packagingFormal.getContract());
            ncProProcessControl.setPcDate(packagingFormal.getOfficialProductionDate());
            ncProProcessControl.setScDate(packagingFormal.getOfficialProductionDate());
            ncProProcessControl.setJhDate(packagingFormal.getJhDate());
            ncProProcessControl.setLineCode(packagingFormal.getLineId());
            ncProProcessControl.setLineName(packagingFormal.getLineName());
            ncProProcessControl.setGwCode(packagingFormal.getGwCode());
            ncProProcessControl.setGwName(packagingFormal.getGwName());
            ncProProcessControl.setDyZcNum(packagingFormal.getDyZcNum());

            if(packagingFormal.getNormalProductionCount()==null){
                ncProProcessControl.setPcNum(new BigDecimal(0));
                ncProProcessControl.setNoPcNum(new BigDecimal(0));

            }else{
                ncProProcessControl.setPcNum(new BigDecimal(packagingFormal.getNormalProductionCount()));
                ncProProcessControl.setNoPcNum(new BigDecimal(packagingFormal.getNormalProductionCount()));
            }

            if(packagingFormal.getOrderNum()==null){
                ncProProcessControl.setOrderNum(new BigDecimal(0));
            }else{
                ncProProcessControl.setOrderNum(new BigDecimal(packagingFormal.getOrderNum()));
            }

            ncProProcessControl.setPlanOnlineTime(packagingFormal.getPlanOnlineTime());
            ncProProcessControl.setPlanOfflineTime(packagingFormal.getPlanOfflineTime());
            ncProProcessControl.setCreateTime(new Date());
            ncProProcessControl.setCreateBy(username);
            ncProProcessControl.setRemark(null);
            ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
            ncPackagingFormalSchedulingPlan.setIsRead("1");
            ncPackagingFormalSchedulingPlan.setId(packagingFormal.getId());
            ncPackagingFormalSchedulingPlanMapper.updateNcPackagingFormalSchedulingPlan(ncPackagingFormalSchedulingPlan);



            List<CcStockInfoVo> stockInfos = ncOrderResourcesYsMapper.getStockInfoBySubCode(packagingFormal.getGwItemCode());

            String kwdm = "";
            for(int i=0;i<stockInfos.size();i++){
                if(i==stockInfos.size()-1){
                    kwdm += stockInfos.get(i).getKuweiCode();
                }else{
                    kwdm += stockInfos.get(i).getKuweiCode()+",";
                }
            }
            NcDistributionPlan ncDistributionPlan = new NcDistributionPlan();
            ncDistributionPlan.setFactoryCode(packagingFormal.getFactoryCode());
            ncDistributionPlan.setFactoryName(packagingFormal.getFactoryName());
            ncDistributionPlan.setOrderType(packagingFormal.getOrderType());
            ncDistributionPlan.setOrderNumber(packagingFormal.getOrderNo());
            ncDistributionPlan.setProductCode(packagingFormal.getProductNumber());
            ncDistributionPlan.setProductName(packagingFormal.getProductName());
            ncDistributionPlan.setOrderQuantity(packagingFormal.getOrderNum());
            ncDistributionPlan.setProductType(packagingFormal.getProType());
            ncDistributionPlan.setMaterialCode(packagingFormal.getGwItemCode());
            ncDistributionPlan.setMaterialName(packagingFormal.getGwItemName());
//            ncDistributionPlan.setMaterialSpecification(productFormal.getItemSpec());
            if(stockInfos.size()>0){
                ncDistributionPlan.setQualityStatus(stockInfos.get(0).getQualityState());
                ncDistributionPlan.setUsageStatus(stockInfos.get(0).getUseState());
                ncDistributionPlan.setWarehouseCode(stockInfos.get(0).getStockId());
            }
            ncDistributionPlan.setUnit(packagingFormal.getUnit());
            ncDistributionPlan.setTotalOrderUsage(BigDecimal.valueOf(packagingFormal.getOrderNum()*packagingFormal.getDtyl()));
            ncDistributionPlan.setPerUnitUsage(BigDecimal.valueOf(packagingFormal.getDtyl()));
            ncDistributionPlan.setDistributionPlanQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
            ncDistributionPlan.setLockedAccountQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
//            ncDistributionPlan.setLockedAccountTime(stockInfos.get(0).lo);
            ncDistributionPlan.setLocationCode(kwdm);
            ncDistributionPlan.setCreateBy(SecurityUtils.getUsername());
            ncDistributionPlan.setCreateTime(new Date());
            ncDistributionPlanMapper.insertNcDistributionPlan(ncDistributionPlan);
        });

        ncAssemblyFormalSchedulingPlans.stream().forEach(assemblyFormal->{
            NcProProcessControl ncProProcessControl = new NcProProcessControl();
            ncProProcessControl.setProType("部件");
            ncProProcessControl.setPcCode(assemblyFormal.getPreProductionNumber());
            ncProProcessControl.setOrderNo(assemblyFormal.getOrderNo());
            ncProProcessControl.setContractNo(assemblyFormal.getContract());
            ncProProcessControl.setPcDate(assemblyFormal.getOfficialProductionDate());
            ncProProcessControl.setScDate(assemblyFormal.getOfficialProductionDate());
            ncProProcessControl.setJhDate(assemblyFormal.getJhDate());
            ncProProcessControl.setLineCode(assemblyFormal.getLineId());
            ncProProcessControl.setLineName(assemblyFormal.getLineName());
            ncProProcessControl.setGwCode(assemblyFormal.getGwCode());
            ncProProcessControl.setGwName(assemblyFormal.getGwName());
            ncProProcessControl.setDyZcNum(assemblyFormal.getDyZcNum());
            if(assemblyFormal.getNormalProductionCount()==null){
                ncProProcessControl.setPcNum(new BigDecimal(0));
                ncProProcessControl.setNoPcNum(new BigDecimal(0));

            }else{
                ncProProcessControl.setPcNum(new BigDecimal(assemblyFormal.getNormalProductionCount()));
                ncProProcessControl.setNoPcNum(new BigDecimal(assemblyFormal.getNormalProductionCount()));
            }

            if(assemblyFormal.getOrderNum()==null){
                ncProProcessControl.setOrderNum(new BigDecimal(0));
            }else{
                ncProProcessControl.setOrderNum(new BigDecimal(assemblyFormal.getOrderNum()));
            }

            ncProProcessControl.setPlanOnlineTime(assemblyFormal.getPlanOnlineTime());
            ncProProcessControl.setPlanOfflineTime(assemblyFormal.getPlanOfflineTime());
            ncProProcessControl.setCreateTime(new Date());
            ncProProcessControl.setCreateBy(username);
            ncProProcessControl.setRemark(null);
            ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
            ncAssemblyFormalSchedulingPlan.setIsRead("1");
            ncAssemblyFormalSchedulingPlan.setId(assemblyFormal.getId());
            ncAssemblyFormalSchedulingPlanMapper.updateNcAssemblyFormalSchedulingPlan(ncAssemblyFormalSchedulingPlan);



            List<CcStockInfoVo> stockInfos = ncOrderResourcesYsMapper.getStockInfoBySubCode(assemblyFormal.getGwItemCode());

            String kwdm = "";
            for(int i=0;i<stockInfos.size();i++){
                if(i==stockInfos.size()-1){
                    kwdm += stockInfos.get(i).getKuweiCode();
                }else{
                    kwdm += stockInfos.get(i).getKuweiCode()+",";
                }
            }
            NcDistributionPlan ncDistributionPlan = new NcDistributionPlan();
            ncDistributionPlan.setFactoryCode(assemblyFormal.getFactoryCode());
            ncDistributionPlan.setFactoryName(assemblyFormal.getFactoryName());
            ncDistributionPlan.setOrderType(assemblyFormal.getOrderType());
            ncDistributionPlan.setOrderNumber(assemblyFormal.getOrderNo());
            ncDistributionPlan.setProductCode(assemblyFormal.getProductNumber());
            ncDistributionPlan.setProductName(assemblyFormal.getProductName());
            ncDistributionPlan.setOrderQuantity(assemblyFormal.getOrderNum());
            ncDistributionPlan.setProductType(assemblyFormal.getProType());
            ncDistributionPlan.setMaterialCode(assemblyFormal.getGwItemCode());
            ncDistributionPlan.setMaterialName(assemblyFormal.getGwItemName());
//            ncDistributionPlan.setMaterialSpecification(productFormal.getItemSpec());

            if(stockInfos.size()>0){
                ncDistributionPlan.setQualityStatus(stockInfos.get(0).getQualityState());
                ncDistributionPlan.setUsageStatus(stockInfos.get(0).getUseState());
                ncDistributionPlan.setWarehouseCode(stockInfos.get(0).getStockId());
            }
            ncDistributionPlan.setUnit(assemblyFormal.getUnit());
            ncDistributionPlan.setTotalOrderUsage(BigDecimal.valueOf(assemblyFormal.getOrderNum()*assemblyFormal.getDtyl()));
            ncDistributionPlan.setPerUnitUsage(BigDecimal.valueOf(assemblyFormal.getDtyl()));
            ncDistributionPlan.setDistributionPlanQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
            ncDistributionPlan.setLockedAccountQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
//            ncDistributionPlan.setLockedAccountTime(stockInfos.get(0).lo);
            ncDistributionPlan.setLocationCode(kwdm);
            ncDistributionPlan.setCreateBy(SecurityUtils.getUsername());
            ncDistributionPlan.setCreateTime(new Date());
            ncDistributionPlanMapper.insertNcDistributionPlan(ncDistributionPlan);

        });

        ncProcessingFormalSchedulingPlans.stream().forEach(processingFormal->{
            NcProProcessControl ncProProcessControl = new NcProProcessControl();
            ncProProcessControl.setFactoryCode(processingFormal.getFactoryCode());
            ncProProcessControl.setFactoryName(processingFormal.getFactoryName());
            ncProProcessControl.setPcCode(processingFormal.getPcId());
            ncProProcessControl.setProType("零件");
            ncProProcessControl.setOrderNo(processingFormal.getOrderNo());
            ncProProcessControl.setContractNo(processingFormal.getContract());
            ncProProcessControl.setPcDate(processingFormal.getPcDate());
            ncProProcessControl.setScDate(processingFormal.getProductionTime());
            ncProProcessControl.setJhDate(processingFormal.getJhDate());
            ncProProcessControl.setLineCode(processingFormal.getLineId());
            ncProProcessControl.setLineName(processingFormal.getLineName());
            ncProProcessControl.setGwCode(processingFormal.getGwCode());
            ncProProcessControl.setGwName(processingFormal.getGwName());
            ncProProcessControl.setDyZcNum(processingFormal.getDyZcNum());

            if(processingFormal.getPcNum()==null){
                ncProProcessControl.setPcNum(new BigDecimal(0));
                ncProProcessControl.setNoPcNum(new BigDecimal(0));

            }else{
                ncProProcessControl.setPcNum(new BigDecimal(processingFormal.getPcNum()));
                ncProProcessControl.setNoPcNum(new BigDecimal(processingFormal.getNoPcNum()));
            }

            if(processingFormal.getOrderNum()==null){
                ncProProcessControl.setOrderNum(new BigDecimal(0));
            }else{
                ncProProcessControl.setOrderNum(new BigDecimal(processingFormal.getOrderNum()));
            }

            ncProProcessControl.setPlanOnlineTime(processingFormal.getPlanOnlineTime());
            ncProProcessControl.setPlanOfflineTime(processingFormal.getPlanOfflineTime());
            ncProProcessControl.setCreateTime(new Date());
            ncProProcessControl.setCreateBy(username);
            ncProProcessControl.setRemark(null);
            ncProProcessControlMapper.insertNcProProcessControl(ncProProcessControl);
            ncProcessingFormalSchedulingPlan.setIsRead("1");
            ncProcessingFormalSchedulingPlan.setId(processingFormal.getId());
            ncProcessingFormalSchedulingPlanMapper.updateNcProcessingFormalSchedulingPlan(ncProcessingFormalSchedulingPlan);


            List<CcStockInfoVo> stockInfos = ncOrderResourcesYsMapper.getStockInfoBySubCode(processingFormal.getGwItemCode());

            String kwdm = "";
            for(int i=0;i<stockInfos.size();i++){
                if(i==stockInfos.size()-1){
                    kwdm += stockInfos.get(i).getKuweiCode();
                }else{
                    kwdm += stockInfos.get(i).getKuweiCode()+",";
                }
            }
            NcDistributionPlan ncDistributionPlan = new NcDistributionPlan();
            ncDistributionPlan.setFactoryCode(processingFormal.getFactoryCode());
            ncDistributionPlan.setFactoryName(processingFormal.getFactoryName());
            ncDistributionPlan.setOrderType(processingFormal.getOrderType());
            ncDistributionPlan.setOrderNumber(processingFormal.getOrderNo());
            ncDistributionPlan.setProductCode(processingFormal.getProductNumber());
            ncDistributionPlan.setProductName(processingFormal.getProductName());
            ncDistributionPlan.setOrderQuantity(processingFormal.getOrderNum());
            ncDistributionPlan.setProductType(processingFormal.getProType());
            ncDistributionPlan.setMaterialCode(processingFormal.getGwItemCode());
            ncDistributionPlan.setMaterialName(processingFormal.getGwItemName());
//            ncDistributionPlan.setMaterialSpecification(productFormal.getItemSpec());
            if(stockInfos.size()>0){
                ncDistributionPlan.setQualityStatus(stockInfos.get(0).getQualityState());
                ncDistributionPlan.setUsageStatus(stockInfos.get(0).getUseState());
                ncDistributionPlan.setWarehouseCode(stockInfos.get(0).getStockId());
            }

            ncDistributionPlan.setUnit(processingFormal.getUnit());
            ncDistributionPlan.setTotalOrderUsage(BigDecimal.valueOf(processingFormal.getOrderNum()*Long.valueOf(processingFormal.getDtyl())));
            ncDistributionPlan.setPerUnitUsage(new BigDecimal(processingFormal.getDtyl()));
            ncDistributionPlan.setDistributionPlanQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
            ncDistributionPlan.setLockedAccountQuantity(ncDistributionPlan.getTotalOrderUsage().longValue());
//            ncDistributionPlan.setLockedAccountTime(stockInfos.get(0).lo);
            ncDistributionPlan.setLocationCode(kwdm);
            ncDistributionPlan.setCreateBy(SecurityUtils.getUsername());
            ncDistributionPlan.setCreateTime(new Date());
            ncDistributionPlanMapper.insertNcDistributionPlan(ncDistributionPlan);
        });

        //写入配送计划
//        NcOrderResourcesYs ncOrderResourcesYs = new NcOrderResourcesYs();
//        ncOrderResourcesYs.setIsPs("0");
//        List<NcOrderResourcesYs> ncOrderResources = ncOrderResourcesYsMapper.selectNcOrderResourcesYsList(ncOrderResourcesYs);
//        ncOrderResources.stream().forEach(ys->{
//            NcDistributionPlan ncDistributionPlan = new NcDistributionPlan();
//            ncDistributionPlan.setFactoryCode(ys.getFactoryCode());
//            ncDistributionPlan.setFactoryName(ys.getFactoryName());
//            ncDistributionPlan.setOrderType(ys.getOrderType());
//            ncDistributionPlan.setOrderNumber(ys.getOrderNo());
//            ncDistributionPlan.setProductCode(ys.getProductCode());
//            ncDistributionPlan.setProductName(ys.getProductName());
//            ncDistributionPlan.setOrderQuantity(ys.getOrderNumber());
//            ncDistributionPlan.setProductType(ys.getProductType());
//            ncDistributionPlan.setMaterialCode(ys.getItemId());
//            ncDistributionPlan.setMaterialName(ys.getItemName());
//            ncDistributionPlan.setMaterialSpecification(ys.getItemSpec());
//            ncDistributionPlan.setQualityStatus(ys.getQualityStatus());
//            ncDistributionPlan.setUsageStatus(ys.getUsageStatus());
//            ncDistributionPlan.setUnit(ys.getUnit());
//            ncDistributionPlan.setTotalOrderUsage(BigDecimal.valueOf(ys.getTotalOrderUsage()));
//            ncDistributionPlan.setPerUnitUsage(BigDecimal.valueOf(ys.getDtyl()));
//            ncDistributionPlan.setDistributionPlanQuantity(ys.getTotalOrderUsage());
//            ncDistributionPlan.setLockedAccountQuantity(ys.getLockedAccountQuantity());
//            ncDistributionPlan.setLockedAccountTime(ys.getLockedTime());
//            ncDistributionPlan.setWarehouseCode(ys.getWarehouseCode());
//            ncDistributionPlan.setLocationCode(ys.getKwbm());
//            ncDistributionPlan.setCreateBy(SecurityUtils.getUsername());
//            ncDistributionPlan.setCreateTime(new Date());
//            ncDistributionPlanMapper.insertNcDistributionPlan(ncDistributionPlan);
//
//            ncOrderResourcesYs.setIsPs("1");
//            ncOrderResourcesYs.setId(ys.getId());
//            ncOrderResourcesYsMapper.updateNcOrderResourcesYs(ncOrderResourcesYs);
//        });
        return 1;
    }


}
